Okay.
The next section in our GUI that we can script is the spectating system.
When a player presses the spectate button, I want to move their camera to follow in a live player on
the map and then give them some buttons to spectate different players.
So inside of our spectate frame, if we enable visibility for this, we're going to give them two different
buttons.
It's going to tell them who they are currently spectating.
And then when they want to stop spectating, they can hit this button.
And then we'll reset their camera to go back rotating around the map again, and we'll put the main
menu back onto their screen.
Since this spectating system is going to be entirely client sided, we only need to script it within
the local script responsible for our starting menu.
So let me go ahead and disable UI visibility and we can open up our starting menu handler.
And then we can go ahead and do is we can go down into our handler section and we'll create another
scope specifically for our spectating system.
Here we can listen to when our spectate button is activated.
And this is where we will move the player's camera onto another character who is on the map on the alive
team, and we'll make sure to stop rotating their camera around the map.
Otherwise, if there is no alive players on the map, then we'll just tell them that we can't spectate
any players and we'll display that message on the button, just like we did for when the players try
to play the game, but they aren't allowed to spawn in yet, so we should create a debounce for this
button to prevent players from spam clicking it.
So up here we'll create a small section for spectating.
And I'll create a variable.
I'll call this my spectate Debounce set that equal to false.
And then I also want to have a variable to keep track of what player we're currently viewing, because
the plan is to get all of the alive players from the alive team and store them in an array.
And then when we press the either the left or right button, we're going to increment or decrement a
number value to move to the next player inside of the array.
So we could call this viewing player, and by default we'll set it to the value of one.
We're also going to want to grab the camera because we're going to have to manipulate it as well.
So we'll get the current camera in the workspace.
And then in order to grab all of the players that are alive in our game, let's go ahead and get the
team service.
And then I want to create a dedicated function for grabbing all of the players that are currently alive.
So we'll call it Get Alive players, and we'll simply return from the alive team.
All the players that are alive.
And we can go ahead and go down here.
When the spectate button is activated, we'll set spectate debounce equal to true.
And then at the top we'll check to see if spectate debounce is already true.
If it is, we'll return.
Otherwise, what we need to do is we need to get all of the alive players in our game.
So we'll call our Get Alive players function.
And if the number of alive players is greater than zero, then we can go ahead and start spectating.
Otherwise, if there are no live players, then what we can do is we can update the text inside of our
spectate button and say something like no players.
To spectate.
And then we'll display that for like three seconds, and then we'll reset the text back to the original.
So I'll make a variable, call it original text.
And that's equal to spectate button dot text.
And then we can set spectate button.
Dot text back to the original text.
And then down here we'll set spectate the bounce back to false.
But if there are enough players to spectate, then what we can go ahead and do is we'll make sure that
the viewing player variable is set to one.
We're going to enable our spectate frame.
So we'll set visibility equal to true.
We want to disable the main guy.
So we'll set boiler image dot visibility equal to false.
And then we want the camera to stop rotating around the map.
So we need to use our camera event.
So I believe it was called what.
Rotate.
Yeah.
Rotate camera event.
We're going to fire and pass false to let it know to stop rotating our camera around the map.
And now what we can do is inside of the camera, we can go ahead and update the camera subject property
and pass the humanoid of the player we want to spectate.
So that's going to be equal to and inside of our live players.
We're going to get the current player at the viewing player index.
We're going to refer to their character and then get their humanoid.
And now that's going to be the subject of our camera.
And then we also need to make sure to update the text inside of our spectate frame to tell us what player
we're currently spectating.
So inside of the spectate frame, we have a text label in there called name for player.
And we're going to update that text equal to this player's name.
So we'll just copy this, paste that there and get the name of the player.
Now that we've done that, we need to go ahead and listen to when those two buttons inside of the spectate
frame are triggered.
So we have to listen to when the player wants to move to the next player to spectate, or the previous,
as well as when they press the stop spectating button.
So what we can go ahead and do is we can go ahead and listen to, for example, in our spectate frame
when the stop button is pressed.
So when that is activated.
What we want to do is we want to disable the spectating frame, set the visibility equal to false.
We want to re-enable the main menu, set the visibility equal to true, and then we want our camera
to start rotating around the map again.
So we're going to use our rotate camera event and fire and say hey start rotating our camera around
the map again.
We also want to listen inside of our spectate frame for the next button.
So next activated we'll connect a function to that.
And then we also want to listen to the spectate frame dot previous button.
When that is activated we'll connect a function to that.
And when we want to move to the next player, we're going to set viewing player plus equal to one.
And then we want to get the current alive players.
So we'll call get alive players.
And if we incremented this value to be greater than how many players are alive on the map.
So if the viewing player is, let's say, greater than the number of alive players, then we want to
reset viewing player back to one.
However, we also want to confirm that there are still alive players on the map when they press the
next or increment button.
So if the number of alive players is equal to zero, then we don't want to do anything at all.
We'll just return.
And then what we can go ahead and do is simply update the camera's camera subject, and we'll just basically
copy what we did up here and just paste it right here.
And then we can copy all of this code and do the exact same thing in the previous button.
We'll paste it here, but this time we want to decrement our viewing player by one.
And then we want to check if the viewing player value becomes less than the value of one.
And if it does, then we want to set the value to the highest value, which is the number of alive players
on the map.
So that way we have a loop.
So if the player keeps spam clicking the next button, it's going to allow them to go in a loop.
And if they keep spam clicking the previous button, it will allow them to go in a loop.
And I believe that's all we need to do.
So to test it out, we can go ahead and create a two player server and see if we can spectate other
players.
Okay here.
I have my two players in my game and if I go and press the spectate button, it should tell me.
There we go.
No players to spectate on both of these players.
Now, if one of them jumps into the map like this one, and then I try to spectate.
As you can see now, it's allowing me to spectate this player and it says I'm spectating player one.
If I hit stop spectating, it resets my camera and starts rotating around the map again and my main
guy is visible again.
If I go back and spectate, I can go ahead and hit these buttons.
And since of course there are no other players that are on the live team, it's not doing anything.
It's just setting it to the one player that's on the map.
But as you can see, our spectating system is working fine.
So our camera follows along where our player is walking, and we're also able to rotate our camera on
this client to fully spectate this other player.
And we can go ahead and watch this player kill these zombies on the map.
Now let's say this player dies.
Let me go ahead and have this zombie kill me.
Hillbilly bang.
These guys are doing a poor job at killing me.
So once these guys kill me, there we go.
As you can see, we're still spectating this player.
But then their character disappears, and now our camera's just stuck here.
And if we click these buttons, of course, nothing's going to happen.
But that's why we have the stop spectating button.
So we can go ahead and press this button and go back to the main menu.
And if we try to spectate again, as you can see, there are no players to spectate.
All right.
And that is how we can implement a spectating system in our game where nearly complete with our project,
we just have to do a few more things.
So I'll see you in the next lecture.